package com.sanpu.kxe.business.repay.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.business.repay.contract.UpdateCreditContract;
import com.sanpu.kxe.business.repay.model.RepayModel;
import com.sanpu.kxe.business.repay.presenter.UpdateCreditPresenter;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.utils.SingleCheckListener;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.VerifyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：修改信用卡信息
 * Created by 薛勇军 on 2019/4/10.
 */

public class UpdateCreditActivity extends BaseTitleActivity implements UpdateCreditContract.View {

    @Bind(R.id.containLayout)
    ScrollView containLayout;
    @Bind(R.id.card_image)
    ImageView vCardImage;
    @Bind(R.id.bank_name)
    TextView bankNameTv;
    @Bind(R.id.bankNumberTv)
    TextView bankNumberTv;
    @Bind(R.id.phoneEt)
    EditTextWithDel phoneEt;
    @Bind(R.id.billDayTv)
    TextView billDayTv;
    @Bind(R.id.refundDayTv)
    TextView refundDayTv;

    private SingleCheckListener singleCheckListener;

    private UpdateCreditContract.Presenter mPresenter;

    private List<String> selectList = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_update_credit;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("修改信用卡");
    }

    @Override
    protected void initData() {
        super.initData();
        BankInfoOutput infoOutput = (BankInfoOutput)
                getIntent().getSerializableExtra(RepayConfig.INTENT_BANKINFO);
        if (infoOutput != null) {
            if (!TextUtils.isEmpty(infoOutput.getBankNo())) {
                bankNumberTv.setText(VerifyUtil.secretBankCardNumber(infoOutput.getBankNo()));
            }
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl() + infoOutput.getLogoPath(), vCardImage);
            if (!TextUtils.isEmpty(infoOutput.getBankName())) {
                bankNameTv.setText(infoOutput.getBankName());
            }
            if (!TextUtils.isEmpty(infoOutput.getBillDay())) {
                billDayTv.setText("每月" + infoOutput.getBillDay() + "号");
            }
            if (!TextUtils.isEmpty(infoOutput.getDueDay())) {
                refundDayTv.setText("每月" + infoOutput.getDueDay() + "号");
            }
            if (!TextUtils.isEmpty(infoOutput.getRsdTel())) {
                phoneEt.setText(infoOutput.getRsdTel());
            }
        }
        selectList = RepayModel.getInstance().getSelectList();
        mPresenter = new UpdateCreditPresenter(this, infoOutput);
    }

    @OnClick({R.id.next, R.id.billDayLl, R.id.refundDayLl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next:
                mPresenter.onCommit(phoneEt.getText().toString(), billDayTv.getText().toString(),
                        refundDayTv.getText().toString());
                break;
            case R.id.billDayLl:
                if (singleCheckListener == null) {
                    singleCheckListener = new SingleCheckListener(UpdateCreditActivity.this);
                }
                singleCheckListener.handleCheck(containLayout, billDayTv, selectList);
                break;
            case R.id.refundDayLl:
                if (singleCheckListener == null) {
                    singleCheckListener = new SingleCheckListener(UpdateCreditActivity.this);
                }
                singleCheckListener.handleCheck(containLayout, refundDayTv, selectList);
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(UpdateCreditContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
